package com.cskaoyan.service.vx;


import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.vxBo.CouponWxBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.wxPo.*;
import com.cskaoyan.bean.vo.vxVo.CouponVO;
import com.cskaoyan.bean.vo.vxVo.CouponWxVO;
import com.cskaoyan.mapper.wxMapper.MarketCartMapper;
import com.cskaoyan.mapper.wxMapper.MarketCouponMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CouponWxServiceImpl implements CouponWxService {
    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCartMapper marketCartMapper;


    @Override
    public CouponVO selectCoupon(BaseParam baseParam) {
        CouponVO couponVO = new CouponVO();
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectCoupon();
        List<MarketCoupon> marketCouponList = new ArrayList<>();
        for (MarketCoupon marketCoupon : marketCoupons) {
            MarketCoupon marketCoupon1 = new MarketCoupon();
            marketCoupon1.setDays(marketCoupon.getDays());
            marketCoupon1.setDesc(marketCoupon.getDesc());
            marketCoupon1.setDiscount(marketCoupon.getDiscount());
            marketCoupon1.setEndTime(marketCoupon.getEndTime());
            marketCoupon1.setId(marketCoupon.getId());
            marketCoupon1.setMin(marketCoupon.getMin());
            marketCoupon1.setName(marketCoupon.getName());
            marketCoupon1.setStartTime(marketCoupon.getStartTime());
            marketCoupon1.setTag(marketCoupon.getTag());
            marketCouponList.add(marketCoupon1);
        }
        PageInfo pageInfo = new PageInfo(marketCoupons);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        couponVO.setTotal((int) total);
        couponVO.setPage(baseParam.getPage());
        couponVO.setPages(pages);
        couponVO.setLimit(baseParam.getLimit());
        couponVO.setList(marketCouponList);
        return couponVO;
    }

    @Override
    public CouponWxVO selectMyCoupon(BaseParam baseParam, Short status) {
        CouponWxVO couponWxVO = new CouponWxVO();
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<CouponWxBO> couponWxPOS = marketCouponMapper.selectMyCoupon(status);
        List<CouponWxBO> couponWxPOS1 = new ArrayList<>();
        for (CouponWxBO couponWxPO : couponWxPOS) {
            CouponWxBO couponWxPO1 = new CouponWxBO();
            couponWxPO1.setAvailable(couponWxPO.getAvailable());
            couponWxPO1.setCid(couponWxPO.getCid());
            couponWxPO1.setDesc(couponWxPO.getDesc());
            couponWxPO1.setDiscount(couponWxPO.getDiscount());
            couponWxPO1.setEndTime(couponWxPO.getEndTime());
            couponWxPO1.setId(couponWxPO.getId());
            couponWxPO1.setMin(couponWxPO.getMin());
            couponWxPO1.setName(couponWxPO.getName());
            couponWxPO1.setStartTime(couponWxPO.getStartTime());
            couponWxPO1.setTag(couponWxPO.getTag());
            couponWxPOS1.add(couponWxPO);
            Date date = new Date();
            if(couponWxPO.getEndTime() != null){
                if(couponWxPO.getEndTime().compareTo(date)<0 && status != 1 ){
                    marketCouponMapper.updateStatus(couponWxPO.getId());
                }
            }
        }
        PageInfo pageInfo = new PageInfo(couponWxPOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        couponWxVO.setTotal((int) total);
        couponWxVO.setPage(baseParam.getPage());
        couponWxVO.setPages(pages);
        couponWxVO.setLimit(baseParam.getLimit());
        couponWxVO.setList(couponWxPOS1);
        return couponWxVO;
    }

    @Override
    public MarketCouponUser insertCoupon(MarketCouponUser marketCouponUser) {
        MarketCoupon marketCoupon = marketCouponMapper.selectReceive(marketCouponUser);
        if(marketCoupon.getType() == 0){
            marketCoupon.setStartTime(new Date());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(marketCoupon.getStartTime());
            calendar.add(Calendar.DATE,10);
            Date d = calendar.getTime();
            marketCoupon.setEndTime(d);
        }
        while (marketCoupon.getType() == 2 || marketCoupon.getType() == 1){
            continue;
         }
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();
        int count = marketCouponMapper.insertReceive(marketCoupon,userId);
        return new MarketCouponUser();
    }

    @Override
    public int ExchangeCoupon(MarketCoupon marketCoupon) {
        //如果是marketCoupon和marketCoupon1 不匹配
        MarketCoupon marketCoupon1 = marketCouponMapper.selectExchange(marketCoupon);
        if(marketCoupon1 == null){
            return 0;
        }
        if(marketCoupon1.getType() == 2){
            marketCoupon1.setStartTime(new Date());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(marketCoupon1.getStartTime());
            calendar.add(Calendar.DATE,7);
            Date d = calendar.getTime();
            marketCoupon1.setEndTime(d);
        }

        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();
        int count = marketCouponMapper.insertExchange(marketCoupon1,userId);
        return count;

    }

    @Override
    public CouponWxVO selectOtherSelectList(BaseParam baseParam, Integer cartId, Integer grouponRulesId, Integer userId) {
        CouponWxVO couponWxBO = new CouponWxVO();
        MarketCart marketCart = marketCartMapper.selectByPrimaryKey(cartId);

        PageHelper.startPage(1, 40);
        List<CouponWxBO> couponWxBOS = marketCouponMapper.selectSelectList(userId);
        List<CouponWxBO> couponWxBOList = new ArrayList<>();
        for (CouponWxBO wxBO : couponWxBOS) {
            if (wxBO.getMin().compareTo(marketCart.getPrice().multiply(BigDecimal.valueOf(marketCart.getNumber()))) < 0 && wxBO.getEndTime().after(new Date()) && wxBO.getStartTime().before(new Date()) ) {
                wxBO.setAvailable(true);
            } else {
                wxBO.setAvailable(false);
            }
        }
        PageInfo pageInfo = new PageInfo(couponWxBOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        couponWxBO.setTotal((int) total);
        couponWxBO.setPage(1);
        couponWxBO.setPages(pages);
        couponWxBO.setLimit((int) total);
        couponWxBO.setList(couponWxBOS);
        return couponWxBO;
    }

}
